Imports DAL
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.Sql
Imports System.Data.SqlClient
Imports System.Object
Imports System.Configuration
Imports Microsoft.VisualBasic.Strings
Imports MyClasses

Public Class frmRestore
    Public bBackRestore As New BBackup
    Public FileExtension As String = "SQLBackup"

    Private Sub frmRestore_HelpButtonClicked(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Me.HelpButtonClicked
        Help.ShowHelp(Me, "Pharma.chm", HelpNavigator.KeywordIndex, "Restore Database")
        e.Cancel = True
    End Sub

    Private Sub frmRestore_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        If e.KeyCode = Keys.F1 Then
            e.SuppressKeyPress = True
            Help.ShowHelp(Me, "Pharma.chm", HelpNavigator.KeywordIndex, "Restore Database")
        End If
    End Sub

    Private Sub frmBackup_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Call BCommon.SetFormStandards(Me, False)
        '********HideProgressBar()
        txtBackupFile.Text = ""
        'LoadSettingsFrom(My.Application.Info.DirectoryPath + "\DefaultValues.restoresettings")
        'txtBackupDir.Text = My.Application.Info.DirectoryPath + "\Temp\" + Format(DateTime.Now, "ddMMyyhhmmss") + "\"
        '        txtBackupDir.Text = "D:\Temp\" + Format(DateTime.Now, "ddMMyyhhmmss") + "\"
        txtBackupDir.Text = My.Application.Info.DirectoryPath.Substring(0, 2) + "\Temp\" + Format(DateTime.Now, "ddMMyyhhmmss") + "\"
        'If My.Computer.FileSystem.DirectoryExists(txtBackupDir.Text) Then
        '    My.Computer.FileSystem.DeleteDirectory(txtBackupDir.Text, FileIO.DeleteDirectoryOption.DeleteAllContents)
        'End If
        ' ''CREATE TEMPORARY DIRECTORY.
        'If Not My.Computer.FileSystem.DirectoryExists(txtBackupDir.Text) Then
        '    My.Computer.FileSystem.CreateDirectory(txtBackupDir.Text)
        'End If
    End Sub

    Private Sub cmdOpenFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOpenFile.Click

        Dim sFile As String = AskOpenFile()
        Try
            If sFile <> "" Then

                txtBackupFile.Text = sFile
                txtFlNm.Text = sFile.Substring(sFile.LastIndexOf("\") + 1, sFile.Length - sFile.LastIndexOf("\") - 1)
                txtLocation.Text = sFile.Substring(0, sFile.LastIndexOf("\") + 1)
            Else
                Exit Sub
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "cmdOpenFile_Click")
        End Try
    End Sub
    Public Function AskOpenFile() As String
        Dim fileD As New OpenFileDialog
        Try
            fileD.Filter = "SQLBackup Files | *." + FileExtension
            If fileD.ShowDialog() = Windows.Forms.DialogResult.OK Then
                Return fileD.FileName
            Else
                Return ""
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "AskOpenFile")
            Return ""
        End Try
    End Function
    Sub Export_Database(ByVal FileName As String)
        Try
            ''CHECK IF BACKUP DIRECTORY EXISTS OR NOT. IF IT EXISTS, THEN DELETE IT.
            ''NOTE THAT THIS BACKUP DIRECTORY IS A TEMPORARY DIRECTORY WHICH WAS INITIALIZED IN DURING FORM LOAD.
            If My.Computer.FileSystem.DirectoryExists(txtBackupDir.Text) Then
                My.Computer.FileSystem.DeleteDirectory(txtBackupDir.Text, FileIO.DeleteDirectoryOption.DeleteAllContents)
            End If
            ''CREATE TEMPORARY DIRECTORY.
            If Not My.Computer.FileSystem.DirectoryExists(txtBackupDir.Text) Then
                My.Computer.FileSystem.CreateDirectory(txtBackupDir.Text)
            End If
            'INITIALIZE A DATATABLE. THIS DATATABLE WILL HOLD ALL DATABASE OBJECTS AND THEIR SCRIPTS.

            '***********SetProgress(4)
            'FOLLOWING PROCEDURE WILL GENERATE SCRIPTS FOR SELECTED TABLES USING SQL-DMO. THIS PROCEDURE ALSO BACK UP DATA USING BCP FUNCTIONS IN SQL-DMO LIBRARY.
            'SCRIPT FILE AND DATA FILES WILL BE SAVED IN TEMPORARY FOLDER.

            'BBackup.GenerateFiles(DTScripts, chkData.Checked, "table", chkStructure.Checked, chkMaster.Checked, chkTrans.Checked, FileName)
            Dim Result As Boolean

            'Result = BBackup.GenerateFiles(txtBackupDir.Text, chkData.Checked, "table", True, chkMaster.Checked, chkTrans.Checked, ChkFull.Checked, chkSystem.Checked, FileName)
            Result = BBackup.GenerateFiles(txtBackupDir.Text, True, "table", True, False, False, ChkFull.Checked, chkSystem.Checked, FileName)

            '***********SetProgress(6)
            My.Computer.FileSystem.DeleteDirectory(txtBackupDir.Text, FileIO.DeleteDirectoryOption.DeleteAllContents)
            '***********SetProgress(8)
            If Result Then
                BCommon.ShowMsgError(10388)
            Else
                BCommon.ShowMsgError(10389)
            End If
            '***********HideProgressBar()
            'Since script for all objects and data are now saved to temporary directory, zip this temporary folder to a file with extension *.SQLBACKUP.
        Catch ex As Exception

        End Try

    End Sub

    'Private Sub btnBackUp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    '    Try
    '        If txtBackupFile.Text = "" Then
    '            MessageBox.Show("Please Select Location", "Pharmacy Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
    '            Exit Sub
    '        End If
    '        If txtFlNm.Text = "" Then
    '            MessageBox.Show("Please Select File", "Pharmacy Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
    '            Exit Sub
    '        End If
    '        If ChkFull.Checked = False And chkSystem.Checked = False Then
    '            MessageBox.Show("Please Select atleast one backup option", "Pharmacy Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
    '            Exit Sub
    '        End If
    '        If txtBackupFile.Text.Contains(".") = True Or txtBackupFile.Text.Contains("\") = True Then
    '            MessageBox.Show("Please give Proper file name", "Pharmacy Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
    '            Exit Sub
    '        End If

    '        cmdExport.Enabled = False
    '        If txtBackupFile.Text.EndsWith("\") = False Then
    '            Export_Database(txtBackupFile.Text + "\" + txtFlNm.Text + ".SQLBackup")
    '        Else
    '            Export_Database(txtBackupFile.Text + txtFlNm.Text + ".SQLBackup")
    '        End If
    '    Catch ex As Exception

    '    End Try
    'End Sub
    Private Sub cmdExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExport.Click

        Try
            If My.Computer.FileSystem.DirectoryExists(txtBackupDir.Text) Then
                My.Computer.FileSystem.DeleteDirectory(txtBackupDir.Text, FileIO.DeleteDirectoryOption.DeleteAllContents)
            End If
            ''CREATE TEMPORARY DIRECTORY.
            If Not My.Computer.FileSystem.DirectoryExists(txtBackupDir.Text) Then
                My.Computer.FileSystem.CreateDirectory(txtBackupDir.Text)
            End If

            cmdExport.Enabled = False
            If Trim(txtBackupFile.Text) = "" Then
                'MessageBox.Show("Please select a backup file to restore.", "Select Backup File", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                BCommon.ShowMsgError(10390)
                cmdExport.Enabled = True
                Exit Sub
            End If

            If Not My.Computer.FileSystem.FileExists(txtBackupFile.Text) Then
                'MessageBox.Show("Specified backup file does not exists.", "File not found", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                BCommon.ShowMsgError(10391)
                cmdExport.Enabled = True
                Exit Sub
            End If


            '********ShowProgressBar()
            '********ProgressBar1.Maximum = 8
            '********SetProgress(4)
            '*******If bBackRestore.RestoreAction(chkStructure.Checked, chkMaster.Checked, chkTrans.Checked, txtBackupFile.Text, txtBackupDir.Text) = 1 Then
            Dim frmPrg As New frmProgressBar
            frmPrg.TopMost = True
            frmPrg.MaximumValue = 10
            'frmPrg.StepValue = 2
            'frmPrg.MarqueeAnimationSpeedValue = 200

            frmPrg.Show()

            'frmPrg.PharmProgressBar.Style = ProgressBarStyle.Marquee
            Dim ByteRestoreAct As Byte
            frmPrg.SetProgress(1)
            frmPrg.SetProgress(2)
            frmPrg.SetProgress(5)
            frmPrg.SetProgress(7)
            ByteRestoreAct = bBackRestore.RestoreAction(True, False, False, ChkFull.Checked, chkSystem.Checked, txtBackupFile.Text, txtBackupDir.Text)
            frmPrg.SetProgress(8)
            frmPrg.SetProgress(10)
            frmPrg.HideProgressbar()

            If ByteRestoreAct = 1 Then
                'MessageBox.Show("Database restore complete", "Restore Complete", MessageBoxButtons.OK, MessageBoxIcon.Information)
                '*************SetProgress(6)
                BCommon.ShowMsgError(10392)
                '*************SetProgress(8)
                cmdExport.Enabled = True
                '*************HideProgressBar()
                Global.System.Windows.Forms.Application.Exit()
                'Me.Close()
            ElseIf ByteRestoreAct = 2 Then
                '****************
                BCommon.ShowMsgError(10432)
                cmdExport.Enabled = True
                Exit Sub
            Else
                'MessageBox.Show("Problem in Database Restore", "Restore Complete", MessageBoxButtons.OK, MessageBoxIcon.Information)
                BCommon.ShowMsgError(10399)
                cmdExport.Enabled = True
                Exit Sub
            End If

        Catch ex As Exception
            'MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop)
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "cmdExport_Click")
        End Try
    End Sub

    Private Sub grpBackUP_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles grpRestore.Enter

    End Sub
    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
        Me.Dispose(True)
    End Sub
End Class